
import time
import logging
import op.op_dk as op_dk
import op.op_dir as op_dir

class Task1_config:
    def __init__(self):
        self.namespace = "default"
        self.ZhongKeda_container_name = "container_name"

        self.Host_source_path = "/host/source/path" 
        self.Dest_container_path = "/container/dest/path" 

class Task1_info:
    def __init__(self):
        self.list_watch_satellite = []

# 配置信息
task1_config = Task1_config()
# 任务信息
task1_info = Task1_info()


#---------------------- 任务 1 : 数据准备 ----------------------
# 启动任务
def run():
    logging.info(">>>>")
    logging.info("Scene-2 task1 start...")
    logging.info("<<<<")

    res_list_watch_satellite =  work1_get_watchSatellite_by_Scheduler()
    task1_info.list_watch_satellite = res_list_watch_satellite
    res_exec =  work2_get_imageData_by_watchSatellite(res_list_watch_satellite)

    while(end_check() == False):
        time.sleep(1)
        pass

    return res_exec
    # return True

# 返回观测星列表
def res():
    return task1_info.list_watch_satellite

# 从数据库中读取 观测卫星列表
def work1_get_watchSatellite_by_Scheduler():
    logging.info(">>>>")
    logging.info("work1 get watchSatellite by Scheduler...")
    list_watch_satellite = {} # TODO : 从数据库中读取 观测卫星列表

    logging.info("<<<<")
    return list_watch_satellite


# 观测卫星容器 获取观测数据
def work2_get_imageData_by_watchSatellite( list_watch_satellite):
    logging.info(">>>>")
    logging.info("work2 get imageData by watchSatellite...")

    # 获取宿主机影像数据目录的文件列表
    file_names, absolute_paths  = op_dir.get_filtered_files_with_paths(
        directory=task1_config.Host_source_path,
        file_extensions=[".jpg", ".png", ".jpeg"],
        filename_contains=["image", "data"],
        include_folders=False
    )

    min_files_satellites = min(len(file_names), len(list_watch_satellite))
    for i in range(min_files_satellites):
        # 从宿主机复制文件到容器
        # op_dk.copy_file_to_container(
        op_dk.KubernetesCLI().copy_file_to_container(
            pod_name        =   list_watch_satellite[i], 
            container_name  =   task1_config.ZhongKeda_container_name, 
            source_path     =   absolute_paths[i], 
            container_path  =   task1_config.Dest_container_path, 
            namespace       =   task1_config.namespace
        )
        
    logging.info("<<<<")
    return True


#检测当前任务是否执行完成, 满足完成条件时，返回True
def end_check():
    logging.info(">>>>")
    logging.info("Scene-2 task3 end check...")
    logging.info("<<<<")
    return True